/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vasthu.core.dao;

import com.vasthu.api.dao.ReturnedDAO;
import com.vasthu.api.domain.ReturnedItem;
import com.vasthu.utility.VstDBManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;

/**
 *
 * @author moremada
 */
public class ReturnedDAOImpl implements ReturnedDAO {

    static Logger logger = Logger.getLogger(PurchasedItemDAOImpl.class);

    @Override
    public boolean insert(ReturnedItem itemToReturn, long billId) {
        if (itemToReturn == null) {
            return false;
        }
        PreparedStatement pst = null;
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("INSERT INTO refund (quantity, discount, sub_total, date, bill_id) "
                    + "VALUES(?, ?, ?, ?, ?)");

            pst.setFloat(1, itemToReturn.getQuantity());
            pst.setFloat(2, itemToReturn.getDiscountRate());
            pst.setFloat(3, itemToReturn.getSubTotal());
            pst.setDate(4, new java.sql.Date(itemToReturn.getDate().getTime()));
            pst.setLong(5, billId);
            pst.executeUpdate();

        } catch (Exception ex) {
            logger.error("unable to insert data to stok_bill : " + ex);
        } finally {
            VstDBManager.get().close();
        }
        return true;
    }
}
